#include <stdio.h>
#include <stdlib.h>
#include <memory.h>


#define B 16
#define N 10

int p = 0, q = 0, r = 0;

void init()
{
    p = 0;
    q = 0;
    r = 0;
}

int getstartbase()
{
    int i = 0;
    int start = 1;
    char intstr[N*3] = {'\0'};

    sprintf(intstr, "%d%d%d", p, q, r);



    for(i = 0; i < strlen(intstr); i++)
    {
        if(intstr[i]-'0' > start)
        {
            start = intstr[i]-'0';
        }
    }
    return start+1;
}

void base10(int base, int s, int* d)
{
    int i = 0;
    char intstr[N] = {'\0'};

    sprintf(intstr, "%d", s);
    *d = 0;
    for(i = 0; i < strlen(intstr); i++)
    {
        *d = *d * base + (intstr[i] - '0');
    }
}

void process()
{
    int k = getstartbase();
    int p10 = 0, q10 = 0, r10 = 0;

    for(; k <= B; k++)
    {
        base10(k, p, &p10);
        base10(k, q, &q10);
        base10(k, r, &r10);

        if(p10 * q10 == r10)
        {
            printf("%d\n", k);
            return;
        }
    }
    printf("0\n");
}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);
    int n = 0, i = 0;

    scanf("%d", &n);
    for(i = 0; i < n; i++)
    {
        scanf("%d%d%d", &p, &q, &r);
        process();
    }


    return 0;
}
